{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "1cbc2310",
   "metadata": {},
   "source": [
    "# 千帆大模型API调用代码模板\n",
    "\n",
    "同济子豪兄 2023-10-19"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0aaf3b34",
   "metadata": {},
   "source": [
    "## 导入工具包"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "6335d992",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "import json"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "09402880",
   "metadata": {},
   "source": [
    "## 传入API Key、Secret Key"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "ddab1711",
   "metadata": {},
   "outputs": [],
   "source": [
    "API_KEY = 'YEeltK****wdjhHq'\n",
    "SECRET_KEY = 'v3W9rN****KYGspR'"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "54e46b74",
   "metadata": {},
   "source": [
    "## 使用 AK，SK 生成鉴权签名（Access Token）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "d4460b57",
   "metadata": {},
   "outputs": [],
   "source": [
    "url = \"https://aip.baidubce.com/oauth/2.0/token\"\n",
    "params = {\"grant_type\": \"client_credentials\", \"client_id\": API_KEY, \"client_secret\": SECRET_KEY}\n",
    "access_token = str(requests.post(url, params=params).json().get(\"access_token\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "fb642b3c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'24.a3fd1c62a1133d9db9208290974ed6d7.2592000.1700712774.282335-41296247'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "access_token"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4ccdc17f",
   "metadata": {},
   "source": [
    "## 填写服务地址"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "d443ef96",
   "metadata": {},
   "outputs": [],
   "source": [
    "serve_url = 'https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/plugin/4jyxfdzpsgg75cdk'"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4071620b",
   "metadata": {},
   "source": [
    "## 指定插件库"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "9fe70b91",
   "metadata": {},
   "outputs": [],
   "source": [
    "plugin_lists = ['uuid-websearch']\n",
    "\n",
    "# plugin_lists = ['uuid-weatherforecast']\n",
    "\n",
    "# plugin_lists = ['uuid-websearch', 'uuid-weatherforecast']"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3a766711",
   "metadata": {},
   "source": [
    "## 访问API"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "30ab6833",
   "metadata": {},
   "outputs": [],
   "source": [
    "# query = '今天上证指数是多少'\n",
    "\n",
    "query = '北京今天天气怎么样'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "b79b06c7",
   "metadata": {},
   "outputs": [],
   "source": [
    "url = serve_url + \"/?access_token=\" + access_token\n",
    "\n",
    "payload = json.dumps({\n",
    "    \"query\": query,\n",
    "    \"plugins\":plugin_lists\n",
    "})\n",
    "headers = {\n",
    "    'Content-Type': 'application/json'\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "da5d85d0",
   "metadata": {},
   "source": [
    "## 解析结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "491eeba2",
   "metadata": {},
   "outputs": [],
   "source": [
    "response = requests.request(\"POST\", url, headers=headers, data=payload)\n",
    "\n",
    "print(response.text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5b14b2f6",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
